<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>squid正向代理 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>squid正向代理</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Squid/">Squid</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos 系统自带 squid 包：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y squid</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然也可以源码包编译安装，<a href="http://www.squid-cache.org/" target="_blank" rel="external">Squid官方网站</a>，以下为 squid 编译参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/squid \</div><div class="line">--<span class="built_in">disable</span>-dependency-tracking \</div><div class="line">--<span class="built_in">enable</span>-dlmalloc \</div><div class="line">--<span class="built_in">enable</span>-gnuregex \</div><div class="line">--<span class="built_in">disable</span>-carp \</div><div class="line">--<span class="built_in">enable</span>-async-io=240 \</div><div class="line">--with-pthreads \</div><div class="line">--<span class="built_in">enable</span>-storeio=ufs,aufs,diskd,null \</div><div class="line">--<span class="built_in">disable</span>-wccp \</div><div class="line">--<span class="built_in">disable</span>-wccpv2 \</div><div class="line">--<span class="built_in">enable</span>-kill-parent-hack \</div><div class="line">--<span class="built_in">enable</span>-cachemgr-hostname=localhost \</div><div class="line">--<span class="built_in">enable</span>-default-err-language=Simplify_Chinese \</div><div class="line">--with-build-environment=POSIX_V6_ILP32_OFFBIG \</div><div class="line">--with-maxfd=65535 \</div><div class="line">--with-aio \</div><div class="line">--<span class="built_in">disable</span>-poll \</div><div class="line">--<span class="built_in">enable</span>-epoll \</div><div class="line">--<span class="built_in">enable</span>-linux-netfilter \</div><div class="line">--<span class="built_in">enable</span>-large-cache-files \</div><div class="line">--<span class="built_in">disable</span>-ident-lookups \</div><div class="line">--<span class="built_in">enable</span>-default-hostsfile=/etc/hosts \</div><div class="line">--with-dl \</div><div class="line">--with-large-files \</div><div class="line">--<span class="built_in">enable</span>-removal-policies=heap,lru \</div><div class="line">--<span class="built_in">enable</span>-delay-pools \</div><div class="line">--<span class="built_in">enable</span>-snmp \</div><div class="line">--<span class="built_in">disable</span>-internal-dns</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 centos 自带的 squid 已经满足需求，所以没必要编译安装。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完成后，可以查看 squid 版本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># squid -v</span></div><div class="line">Squid Cache: Version 3.1.23</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同时还可以看到 squid 的编译参数。下面配置 squid，来实现正向代理：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># rm -f /etc/squid/squid.conf</span></div><div class="line">[root@192 ~]<span class="comment"># vim /etc/squid/squid.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不实用默认的配置文件，删除以后重新写入以下配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">http_port 3128</div><div class="line">acl manager proto cache_object</div><div class="line">acl localhost src 127.0.0.1/32 ::1</div><div class="line">acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1</div><div class="line">acl localnet src 10.0.0.0/8     <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl localnet src 172.16.0.0/12  <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl localnet src 192.168.0.0/16 <span class="comment"># RFC1918 possible internal network</span></div><div class="line">acl SSL_ports port 443</div><div class="line">acl Safe_ports port 80 8080 <span class="comment"># http</span></div><div class="line">acl Safe_ports port 21 <span class="comment"># ftp</span></div><div class="line">acl Safe_ports port 443 <span class="comment"># https</span></div><div class="line">acl CONNECT method CONNECT</div><div class="line">http_access allow manager localhost</div><div class="line">http_access deny manager</div><div class="line">http_access deny !Safe_ports</div><div class="line">http_access deny CONNECT !SSL_ports</div><div class="line">http_access allow localnet</div><div class="line">http_access allow localhost</div><div class="line">http_access allow all</div><div class="line">cache_dir aufs /data/cache 1024 16 256</div><div class="line">cache_mem 128 MB</div><div class="line">hierarchy_stoplist cgi-bin ?</div><div class="line">coredump_dir /var/spool/squid</div><div class="line">refresh_pattern ^ftp: 1440 20% 10080</div><div class="line">refresh_pattern ^gopher: 1440 0% 1440</div><div class="line">refresh_pattern -i (/cgi-bin/|\?) 0 0% 0</div><div class="line">refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload</div><div class="line">refresh_pattern . 0 20% 4320</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件中，第一行的 “http_port 3128” 这个指的是，squid 服务启动后将要监听的端口，也可以是 80 。“cache_dir” 这个用来指定本地磁盘上的缓存目录，后边的1024为大小，单位是 M 具体根据磁盘大小决定。“cache_mem”它用来规定缓存占用内存的大小，即把缓存的东西存到内存里，具体也需要根据机器的内存定，如果只跑 squid 服务，那么留给系统 512M 内存外，其他可以都分给 squid。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件保存后，可以先检测一下是否有语法错误：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># squid -kcheck</span></div><div class="line">squid: ERROR: No running copy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果提示 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">squid: ERROR: No running copy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是 squid 还未启动，不过没有关系，显示成这样说明配置文件没有问题了。在启动前还得初始化缓存目录：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /data/cache</span></div><div class="line">[root@192 ~]<span class="comment"># chown -R squid:squid /data/cache</span></div><div class="line">[root@192 ~]<span class="comment"># squid -z</span></div><div class="line">2017/03/30 20:36:42| Creating Swap Directories</div><div class="line">2017/03/30 20:36:42| /data/cache exists</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/00</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/01</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/02</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/03</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/04</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/05</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/06</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/07</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/08</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/09</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0A</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0B</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0C</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0D</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0E</div><div class="line">2017/03/30 20:36:42| Making directories <span class="keyword">in</span> /data/cache/0F</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;初始化完成后，就可以启动 squid 了：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid start</span></div><div class="line">正在启动 squid：. [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 squid 是否启动：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># ps aux | grep squid</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在可以在真机上测测看 squid 的正向代理了，需要设置 IE 选项，或者直接用 curl 命令测试：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果看到这样一大串，说明 squid 正向代理设置 OK 了，另外也可以观察 squid 对图片的缓存</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/03.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连续访问两次论坛的 logo 图片，可以发现前后两次的不同，其中“X-Cache-Lookup: HIT from 192.168.0.73:3128”显示，该请求已经 HIT ，它直接从本地的3128端口获取了数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候还有这样的需求，就是想限制某些域名不能通过代理访问，或者说只想代理某几个域名，在 squid.conf 中找到：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">acl CONNECT method CONNECT</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在其下面添加四行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">acl http proto HTTP</div><div class="line">acl good_domain dstdomain .apelearn.com .aminglinux.com</div><div class="line">http_access allow http good_domain</div><div class="line">http_access deny http !good_domain</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中白名单域名为“.apelearn.com .aminglinux.com”，这里的 . 表示万能匹配。前面可以是任何字符，只需要填写白名单域名即可。重启 squid 再来测测：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid restart</span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com/ -I</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;访问百度已经变为403了，如果要设置黑名单，道理是一样的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">acl http proto HTTP</div><div class="line">acl bad_domain dstdomain .sina.com .souhu.com</div><div class="line">http_access allow http !bad_domain</div><div class="line">http_access deny http bad_domain</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 squid 后，测试：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/squid restart </span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.sina.com/ -I</span></div><div class="line">[root@192 ~]<span class="comment"># curl -xlocalhost:3128 http://www.baidu.com/ -I</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/squid%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;baidu.com 可以访问，而 sina.com 不可以访问了</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Squid/2. squid正向代理/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
